package org.chunchu.todo.dao;

import javax.inject.Inject;
import javax.inject.Provider;

import org.chunchu.todo.LogUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

;
/**
 * This creates/opens the database.
 */
public class DatabaseOpenHelper extends SQLiteOpenHelper {
	private static final String TAG = DatabaseOpenHelper.class.getName();
	public static final String DATABASE_NAME = "tasksDatabase";
	public static final int DATABASE_VERSION = 9;

	private static final String CREATE_TABLE_TASK = " CREATE TABLE "
			+ TaskDao.T_TASK + " (" + BaseColumns._ID
			+ " integer primary key autoincrement, " + TaskDao.F_NAME
			+ " text, " + TaskDao.F_DETAIL + " text, " + TaskDao.F_CATEGORY_ID
			+ " integer);";

	private static final String CREATE_TABLE_CATEGORY = " CREATE TABLE "
			+ CategoryDao.T_CATEGORY + " (" + BaseColumns._ID
			+ " integer primary key autoincrement, " + CategoryDao.F_NAME
			+ " text);";

	@Inject
	protected static Provider<Context> contextProvider;

	DatabaseOpenHelper() {
		super(contextProvider.get(), DATABASE_NAME, null, DATABASE_VERSION);
	}

	public SQLiteDatabase getDatabase() {
		LogUtils.d("getWritableDatabase");
		try {
			return this.getWritableDatabase();
		} catch (Exception ex) {
			LogUtils.e(ex, "getWritableDatabase error: ", ex.getMessage());
			throw new IllegalStateException("Error opening database");
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		LogUtils.d("Entering OnCreate");
		db.execSQL(CREATE_TABLE_CATEGORY);
		db.execSQL(CREATE_TABLE_TASK);

		addCategory(db, "All Tasks");

		addTask(db, 1, "First", "Love this app");
		LogUtils.d("Success OnCreate");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		LogUtils.d("Entering OnUpgrade");
		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TaskDao.T_TASK + ";");
		db.execSQL("DROP TABLE IF EXISTS " + CategoryDao.T_CATEGORY + ";");
		onCreate(db);
		LogUtils.d("Success OnUpgrade");
	}

	protected long addCategory(SQLiteDatabase database, String name) {
		LogUtils.d("Adding category name: ", name);
		ContentValues initialValues = new ContentValues();
		initialValues.put(TaskDao.F_NAME, name);

		return database.insert(CategoryDao.T_CATEGORY, null, initialValues);
	}

	public long addTask(SQLiteDatabase database, long category, String name,
			String detail) {
		LogUtils.d("Adding task : ", category, name, detail);
		ContentValues initialValues = new ContentValues();
		initialValues.put(TaskDao.F_NAME, name);
		initialValues.put(TaskDao.F_DETAIL, detail);
		initialValues.put(TaskDao.F_CATEGORY_ID, category);

		return database.insert(TaskDao.T_TASK, null, initialValues);
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		super.finalize();
	}

}
